package com.woniu.user.service.impl;

import com.woniu.entity.Result;
import com.woniu.user.mapper.UserMapper;
import com.woniu.user.mapper.UserArticleCollectionMapper;
import com.woniu.user.pojo.User;
import com.woniu.user.pojo.UserArticleCollection;
import com.woniu.user.service.IUserArticleCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户收藏表 服务实现类
 * </p>
 *
 * @author 何荣卓
 * @since 2023-02-17
 */
@Service
public class UserArticleCollectionServiceImpl extends ServiceImpl<UserArticleCollectionMapper, UserArticleCollection> implements IUserArticleCollectionService {

    @Autowired(required = false)
    private UserArticleCollectionMapper userArticleCollectionMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Override
    public Result attentionByUser(String userPhone, String articleId) {
        User user = userMapper.selectByPhone(userPhone);

        UserArticleCollection userArticleCollection = userArticleCollectionMapper.selectIsAttention(user.getId(), articleId);

        if (userArticleCollection == null) {
            UserArticleCollection userArticleCollection1 = new UserArticleCollection();
            userArticleCollection1.setUserId(user.getId());
            userArticleCollection1.setArticleId(articleId);
            int insert = userArticleCollectionMapper.insert(userArticleCollection1);
            if (insert > 0) {
                return Result.ok(200, "收藏成功");
            }
        }

        if (userArticleCollection.getIsDeleted() == 0) {
            return Result.error(200, "你已经收藏过了");
        }
        if (userArticleCollection.getIsDeleted() == 1) {
            int update = userArticleCollectionMapper.updateIsDeleted(user.getId(), articleId);
            if (update > 0) {
                return Result.ok(200, "收藏成功!");
            }
            return Result.error(500);
        }
        return Result.error(500);
    }

    @Override
    public Result rmlikeArticle(String userPhone, String articleId) {
        User user = userMapper.selectByPhone(userPhone);

        int IsFans = userArticleCollectionMapper.rmlikeArticle(user.getId(), articleId);
        if (IsFans > 0){
            return Result.ok(200,"已取消收藏");
        }
        return Result.error(500);
    }
}
